<?php

namespace Controller\Passport;

class Forget extends \Controller\Base {

	public function actionIndex() {
		$msg = '';
		$this->setMeta('找回密码');
		if ($this->isPost()) {
			$msg = $this->doIt();
		}
		$this->assign('msg', $msg);
		$this->display();
	}

	private function doIt() {
		$email = $this->postParam('email');
		$email = trim($email);
		if (!$email) {
			return '请填写登录邮箱！';
		}
		$email = strtolower($email);
		// 验证email格式
		if (!\Util\Comm::checkEmailFormat($email)) {
			return '请填写正确的邮箱！';
		}
		$uid = \Business\User::instance()->getUidByEmail($email);
		if (!$uid) {
			return '该邮箱尚未注册！';
		}
		$token = \Business\User::instance()->getUserForgetPassword($uid, $encodeUid);
		$encodeTime = \Util\Comm::encryptId(time());

		$url = $_SERVER['HTTP_ORIGIN'] . '/Passport/ResetPassword';
		$url .= '?time=' . $encodeTime;
		$url .= '&uid=' . $encodeUid;
		$url .= '&token=' . $token;

		$controller = new \Controller\UC\Email;
		$controller->setTemplate('UC/Forget');
		$controller->assign('url', $url);
		$controller->assign('email', $email);
		$html = $controller->run();
		$mail = new \Lib\Core\Mail();
		$subject = '重新设置天府标局密码';
		$mail->sendMail($email, $subject, $html);
		return '请去邮箱点';
	}
}